import { formatEmpty } from '@/filter/format-empty'
import dictApi from '@/request/dict'

export interface DictItem {
  code: string
  label: string
  order: number
}

export interface Dictionary {
  [index: string]: DictItem[]
}

const DICT: Dictionary = {}

export function setDict(dict: Dictionary) {
  Object.assign(DICT, dict)
  log(DICT)
}

export function getDict(index: string) {
  return DICT[index]
}

export function formatDict(val: string, type: string) {
  let dict = DICT[type]
  if (dict) {
    let item = dict.find(item => item.code == val)
    if (item) {
      return item.label
    }
  }
  return formatEmpty('')
}

export async function loadDict(keys: string) {
  setDict(await dictApi.loadDict(keys))
}
